﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections;
using System.Xml.Linq;
namespace CricketHub
{
    public partial class CountryDetails1 : PhoneApplicationPage
    {
        webRequesting_country obj = new webRequesting_country();
        public CountryDetails1()
        {
            InitializeComponent();
            obj.webRequest();
            obj.det = this;
            country_news.Visibility = Visibility.Visible;
            var cuntyvalue = App.Current as App;
                string cvalue = cuntyvalue.countries;
                ApplicationTitle.Text = cvalue.ToUpper()+"  NEWS";
        }
        List<CountryContent> countrydata = new List<CountryContent>();
        public void UpdateData(string News)
        {
            var element = XElement.Parse(News);
            var val = element.Descendants("item").Count();
            System.Diagnostics.Debug.WriteLine("items count.....count....." + val);
            for (int i = 0; i < val; i++)
            {
                var nextelement = element.Descendants("item").ElementAt(i);
                CountryContent obj = new CountryContent();
                obj.title = nextelement.Element("title").Value;
                obj.link = nextelement.Element("link").Value;
                obj.description = nextelement.Element("description").Value;
                obj.pubDate = nextelement.Element("pubDate").Value;
                countrydata.Add(obj);
                System.Diagnostics.Debug.WriteLine("value for title is........." + obj.title + "value for description........" + obj.description + "value for link is....." + obj.link);
            }
            Deployment.Current.Dispatcher.BeginInvoke(CreateListBox);
 
        }
        private void CreateListBox()
        {
            CreateList();
            LoadList();
            country_news.Visibility = Visibility.Collapsed;
 
        }
        ListBox newsList ;//= new ListBox();
        private void CreateList()
        {
            newsList = new ListBox();
            newsList.Name = "listbox";
            newsList.Height = 630;
            newsList.Width = 450;
            newsList.VerticalAlignment = VerticalAlignment.Top;
            newsList.Background = new SolidColorBrush(Colors.Transparent);
            ContentPanel.Children.Add(newsList);
            newsList.SelectionChanged+=new SelectionChangedEventHandler(newsList_SelectionChanged);


 
        }
        private void newsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CountryContent cuntdat = countrydata.ElementAt(newsList.SelectedIndex);
            App a = App.Current as App;
            a.link = cuntdat.link;
            NavigationService.Navigate(new Uri("/Webbrowser.xaml", UriKind.RelativeOrAbsolute));
 
        }

        private void LoadList()
        {
            for (int index = 0; index < countrydata.Count; index++)
            {
                CountryContent content = countrydata.ElementAt(index);
                StackPanel stk = new StackPanel();
                stk.Name = "stack";
                stk.Height = 153;
                stk.VerticalAlignment = VerticalAlignment.Top;
                stk.HorizontalAlignment = HorizontalAlignment.Left;
                TextBlock txtblk = new TextBlock();
                txtblk.Name = "text" + index.ToString();
                txtblk.Text = content.title;
                //txtblk.LineHeight = 40;
                txtblk.FontWeight = FontWeights.Bold;
                txtblk.FontSize = 25;
                txtblk.Foreground = new SolidColorBrush(Colors.Blue);
                txtblk.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                txtblk.HorizontalAlignment = HorizontalAlignment.Left;
                TextBlock txtblk2=new TextBlock();
                txtblk2.Name = "txt" + index.ToString();
                txtblk2.FontSize = 20;
                txtblk2.Text = content.description+". . .";
                txtblk2.TextWrapping = TextWrapping.Wrap;
                txtblk2.Foreground = new SolidColorBrush(Colors.Black);
                txtblk2.VerticalAlignment = VerticalAlignment.Center;
                txtblk2.HorizontalAlignment = HorizontalAlignment.Left;
                txtblk2.FontWeight = FontWeights.Bold;
                txtblk2.MaxHeight = 60;


                TextBlock txtblk3 = new TextBlock();
                txtblk3.Name = "txt1" + index.ToString();
                txtblk3.FontSize = 18;
                txtblk3.VerticalAlignment = VerticalAlignment.Top;
                txtblk3.HorizontalAlignment = HorizontalAlignment.Left;
                txtblk3.Text = content.pubDate;
                txtblk3.Foreground = new SolidColorBrush(Colors.Black);
                txtblk3.FontWeight = FontWeights.SemiBold;
                Line lne1 = new Line();
                lne1.Name = "line" + index.ToString();
                lne1.X1 = 0;
                lne1.Y1 = 0;
                lne1.X2 = 450;
                lne1.Y2 = 0;
                lne1.StrokeThickness = 12;
                lne1.Stroke = new SolidColorBrush(Colors.Transparent);
                
                Line lne = new Line();
                lne.Name = "line" + index.ToString();
                lne.X1 = 0;
                lne.Y1 = 0;
                lne.X2 = 450;
                lne.Y2 = 0;
                lne.StrokeThickness = 7;
                lne.Stroke = new SolidColorBrush(Colors.Green);
                stk.Children.Add(lne);
                stk.Children.Add(lne1);
                stk.Children.Add(txtblk3);
                stk.Children.Add(txtblk);
                stk.Children.Add(txtblk2);
                //stk.Children.Add(lne1);
                
                
                newsList.Items.Add(stk);
 
                    


 
            }

 
        }
    }
}